package com.jeesite.modules.dataview.chaierweijin.service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import com.jeesite.common.config.Global;
import com.jeesite.common.lang.DateUtils;
import com.jeesite.common.service.CrudService;
import com.jeesite.common.service.ServiceException;
import com.jeesite.common.utils.excel.ExcelImport;
import com.jeesite.common.validator.ValidatorUtils;
import com.jeesite.modules.dataview.chaierweijin.entity.ImportResult;
import com.jeesite.modules.home.entity.EchartsDto;
import org.springframework.stereotype.Service;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.QueryService;
import com.jeesite.modules.dataview.chaierweijin.entity.DcSupImportData;
import com.jeesite.modules.dataview.chaierweijin.dao.DcSupImportDataDao;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

/**
 * 拆而未尽表Service
 * @author dbl
 * @version 2022-08-04
 */
@Service
@Transactional(readOnly=false)
public class DcSupImportDataService extends CrudService<DcSupImportDataDao, DcSupImportData> {
	
	/**
	 * 获取单条数据
	 * @param dcSupImportData
	 * @return
	 */
	@Override
	public DcSupImportData get(DcSupImportData dcSupImportData) {
		return super.get(dcSupImportData);
	}
	
	/**
	 * 查询分页数据
	 * @param dcSupImportData 查询条件
	 * @return
	 */
	@Override
	public Page<DcSupImportData> findPage(DcSupImportData dcSupImportData) {
		return super.findPage(dcSupImportData);
	}
	
	/**
	 * 查询列表数据
	 * @param dcSupImportData
	 * @return
	 */
	@Override
	public List<DcSupImportData> findList(DcSupImportData dcSupImportData) {
		return super.findList(dcSupImportData);
	}

	/**
	 * 保存数据（插入或更新）
	 * @param dcSupImportData
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(DcSupImportData dcSupImportData) {
		super.save(dcSupImportData);
	}

	/**
	 * 删除数据
	 * @param dcSupImportData
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(DcSupImportData dcSupImportData) {
		super.delete(dcSupImportData);
	}

	@Transactional(readOnly=false)
	public String importData(MultipartFile file, Date c28) {


		if (c28==null){
			c28 = new Date();
		}
		//先删除导入时间的数据
		String c28Str = DateUtils.formatDate(c28,"yyyy-MM-dd");
		dao.deleteByC28(c28Str);
		if (file == null){
			throw new ServiceException(text("请选择导入的数据文件！"));
		}
		int successNum = 0; int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		try(ExcelImport ei = new ExcelImport(file, 4, 0)){
			List<DcSupImportData> list = ei.getDataList(DcSupImportData.class);
			for (DcSupImportData importData : list) {
				try{
					// 验证数据文件
					importData.setC28(c28);
					ValidatorUtils.validateWithException(importData);
					this.save(importData);
					successNum++;
					successMsg.append("<br/>" + successNum + "、序号 " + importData.getC1() + " 导入成功");
				} catch (Exception e) {
					failureNum++;
					String msg = "<br/>" + failureNum + "、序号 " + importData.getC1() + " 导入失败：";
					if (e instanceof ConstraintViolationException){
						ConstraintViolationException cve = (ConstraintViolationException)e;
						for (ConstraintViolation<?> violation : cve.getConstraintViolations()) {
							msg += Global.getText(violation.getMessage()) + " ("+violation.getPropertyPath()+")";
						}
					}else{
						msg += e.getMessage();
					}
					failureMsg.append(msg);
					logger.error(msg, e);
				}
			}
		} catch (Exception e) {
			failureMsg.append(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}else{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	public String getNewDate() {
		String newDate ="";
		DcSupImportData dc = dao.getNewDate();
		if(dc!=null){
			newDate= dao.getNewDate().getC28Str();
		}
		return newDate;
	}

	public ImportResult query1(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		ImportResult map = dao.query1(dcSupImportData);
		if (map.getC12()==0){
			map.setC1("0");
		}else{
			Integer param = map.getC12()+map.getC13();
			BigDecimal b = new BigDecimal((float)map.getC12()/param*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			map.setC1(b+"%");
		}

		return map;
	}
	public Map<String, Object> query2(DcSupImportData dcSupImportData) {
		List<EchartsDto> list = dao.query2(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("list",list);
		return map;
	}

	public Page<DcSupImportData> query3(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<DcSupImportData> list = dao.query3(dcSupImportData);
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getParam3()==0){
				list.get(i).setParam5("0");
			}else{
				Integer param = list.get(i).getParam3()+list.get(i).getParam4();
				BigDecimal b = new BigDecimal((double)list.get(i).getParam3()/param*100);
				b= b.setScale(2,BigDecimal.ROUND_HALF_UP);
				list.get(i).setParam5(b+"%");
			}
			if (list.get(i).getParam6()==0){
				list.get(i).setParam8("0");
			}else{
				Integer param = list.get(i).getParam6()+list.get(i).getParam7();
				BigDecimal b = new BigDecimal((double)list.get(i).getParam6()/param*100);
				b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
				list.get(i).setParam8(b+"%");
			}

		}
		return dcSupImportData.getPage().setList(list);
	}

	public Map<String, Object> query4(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<EchartsDto> list = dao.query4(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		map.put("nameList",list.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueList",list.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("list",list);
		return map;
	}

	public Map<String, Object> query5(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<ImportResult> list = dao.query5(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		List<ImportResult> jinhu = list.stream().filter(e->"镜湖区".equals(e.getC1())).collect(Collectors.toList());
		if(jinhu.size()>0){ map.put("jinhu",jinhu.get(0));}else{
			ImportResult result = new ImportResult();
			result.setC1("镜湖区");
			result.setC10(0);
			result.setC11(0);
			result.setC12(0);
			result.setC13(0);
			map.put("jinhu",result);
		}
		List<ImportResult> yijiang = list.stream().filter(e->"弋江区".equals(e.getC1())).collect(Collectors.toList());
		if(yijiang.size()>0){ map.put("yijiang",yijiang.get(0));}else{
			ImportResult result = new ImportResult();
			result.setC1("弋江区");
			result.setC10(0);
			result.setC11(0);
			result.setC12(0);
			result.setC13(0);
			map.put("yijiang",result);
		}
		List<ImportResult> jiujiang = list.stream().filter(e->"鸠江区".equals(e.getC1())).collect(Collectors.toList());
		if(jiujiang.size()>0){ map.put("jiujiang",jiujiang.get(0));}else{
			ImportResult result = new ImportResult();
			result.setC1("鸠江区");
			result.setC10(0);
			result.setC11(0);
			result.setC12(0);
			result.setC13(0);
			map.put("jiujiang",result);
		}
		List<ImportResult> sanshan = list.stream().filter(e->"三山经济开发区".equals(e.getC1())).collect(Collectors.toList());
		if(sanshan.size()>0){ map.put("snashan",sanshan.get(0));}else{
			ImportResult result = new ImportResult();
			result.setC1("三山经济开发区");
			result.setC10(0);
			result.setC11(0);
			result.setC12(0);
			result.setC13(0);
			map.put("snashan",result);
		}

		return map;
	}

	public Map<String, Object> query6(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<EchartsDto> list = dao.query6(dcSupImportData);
		List<EchartsDto> jinhu = list.stream().filter(e->"镜湖区".equals(e.getNameId())).collect(Collectors.toList());
		List<EchartsDto> yijiang = list.stream().filter(e->"弋江区".equals(e.getNameId())).collect(Collectors.toList());
		List<EchartsDto> jiujiang = list.stream().filter(e->"鸠江区".equals(e.getNameId())).collect(Collectors.toList());
		List<EchartsDto> sanshan = list.stream().filter(e->"三山经济开发区".equals(e.getNameId())).collect(Collectors.toList());
		Map<String,Object> map = new HashMap<>();
		map.put("nameListjinhu",jinhu.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueListjinhu",jinhu.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("listjinhu",jinhu);

		map.put("nameListyijiang",yijiang.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueListyijiang",yijiang.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("listyijiang",yijiang);

		map.put("nameListjiujiang",jiujiang.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueListjiujiang",jiujiang.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("listjiujiang",jiujiang);

		map.put("nameListsanshan",sanshan.stream().map(e->e.getName()).collect(Collectors.toList()));
		map.put("valueListsanshan",sanshan.stream().map(e->e.getValue()).collect(Collectors.toList()));
		map.put("listsanshan",sanshan);

		return map;
	}

	public Page<DcSupImportData> query7(DcSupImportData dcSupImportData) {
		dcSupImportData.setC28Str(this.getNewDate());
		List<DcSupImportData> list =  dao.query7(dcSupImportData);
		return dcSupImportData.getPage().setList(list);
	}

	public Map<String, Object> query8(DcSupImportData dcSupImportData) {
		List<ImportResult> list = dao.query8(dcSupImportData);
		Map<String,Object> map = new HashMap<>();
		List<ImportResult> jinhu = list.stream().filter(e->"镜湖区".equals(e.getC1())).collect(Collectors.toList());
		if(jinhu.size()>0){
			BigDecimal b = new BigDecimal((double)jinhu.get(0).getC11()/jinhu.get(0).getC10()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			jinhu.get(0).setC2(b+"%");
			BigDecimal c = new BigDecimal((double)jinhu.get(0).getC12()/jinhu.get(0).getC10()*100);
			c = c.setScale(2,BigDecimal.ROUND_HALF_UP);
			jinhu.get(0).setC3(c+"%");
			jinhu.get(0).setC28(b);
			jinhu.get(0).setC29(c);
			map.put("jinhu",jinhu.get(0));
		}else{
			ImportResult importResult = new ImportResult();
			importResult.setC1("镜湖区");
			importResult.setC2("");
			importResult.setC3("");
			importResult.setC28(BigDecimal.valueOf(0));
			importResult.setC29(BigDecimal.valueOf(0));
			map.put("jinhu",importResult);
		}
		List<ImportResult> yijiang = list.stream().filter(e->"弋江区".equals(e.getC1())).collect(Collectors.toList());
		if(yijiang.size()>0){
			BigDecimal b = new BigDecimal((double)yijiang.get(0).getC11()/yijiang.get(0).getC10()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			yijiang.get(0).setC2(b+"%");
			BigDecimal c = new BigDecimal((double)yijiang.get(0).getC12()/yijiang.get(0).getC10()*100);
			c = c.setScale(2,BigDecimal.ROUND_HALF_UP);
			yijiang.get(0).setC3(c+"%");
			yijiang.get(0).setC28(b);
			yijiang.get(0).setC29(c);
			map.put("yijiang",yijiang.get(0));
		}else {
			ImportResult importResult = new ImportResult();
			importResult.setC1("弋江区");
			importResult.setC2("");
			importResult.setC3("");
			importResult.setC28(BigDecimal.valueOf(0));
			importResult.setC29(BigDecimal.valueOf(0));
			map.put("yijiang",importResult);
		}
		List<ImportResult> jiujiang = list.stream().filter(e->"鸠江区".equals(e.getC1())).collect(Collectors.toList());
		if(jiujiang.size()>0){
			BigDecimal b = new BigDecimal((double)jiujiang.get(0).getC11()/jiujiang.get(0).getC10()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			jiujiang.get(0).setC2(b+"%");
			BigDecimal c = new BigDecimal((double)jiujiang.get(0).getC12()/jiujiang.get(0).getC10()*100);
			c = c.setScale(2,BigDecimal.ROUND_HALF_UP);
			jiujiang.get(0).setC3(c+"%");
			jiujiang.get(0).setC28(b);
			jiujiang.get(0).setC29(c);
			map.put("jiujiang",jiujiang.get(0));
		}else{
			ImportResult importResult = new ImportResult();
			importResult.setC1("鸠江区");
			importResult.setC2("");
			importResult.setC3("");
			importResult.setC28(BigDecimal.valueOf(0));
			importResult.setC29(BigDecimal.valueOf(0));
			map.put("jiujiang",importResult);
		}
		List<ImportResult> sanshan = list.stream().filter(e->"三山经济开发区".equals(e.getC1())).collect(Collectors.toList());
		if(sanshan.size()>0){
			BigDecimal b = new BigDecimal((double)sanshan.get(0).getC11()/sanshan.get(0).getC10()*100);
			b = b.setScale(2,BigDecimal.ROUND_HALF_UP);
			sanshan.get(0).setC2(b+"%");
			BigDecimal c = new BigDecimal((double)sanshan.get(0).getC12()/sanshan.get(0).getC10()*100);
			c = c.setScale(2,BigDecimal.ROUND_HALF_UP);
			sanshan.get(0).setC3(c+"%");
			sanshan.get(0).setC28(b);
			sanshan.get(0).setC29(c);
			map.put("snashan",sanshan.get(0));
		}else{
			ImportResult importResult = new ImportResult();
			importResult.setC1("三山经济开发区");
			importResult.setC2("");
			importResult.setC3("");
			importResult.setC28(BigDecimal.valueOf(0));
			importResult.setC29(BigDecimal.valueOf(0));
			map.put("snashan",importResult);
		}

		return map;
	}
}